<template>
  <el-form label-width="100px" label-position="left" :model="formData" :rules="formRules">
    <el-form-item label="大屏名称" prop="name">
      <el-input
        v-model.trim="formData.name"
        size="default"
        style="width: 100%"
        placeholder="请输入大屏名称"
        @change="changed($event, 'name')"
      />
    </el-form-item>
    <el-form-item label="大屏宽度" prop="width">
      <el-input-number
        v-model.trim="formData.width"
        size="default"
        style="width: 100%"
        controls-position="right"
        @change="changed($event, 'width')"
      />
    </el-form-item>
    <el-form-item label="大屏高度" prop="height">
      <el-input-number
        v-model.trim="formData.height"
        size="default"
        style="width: 100%"
        controls-position="right"
        @change="changed($event, 'height')"
      />
    </el-form-item>

    <el-form-item label="大屏简介" prop="description">
      <el-input
        v-model.trim="formData.description"
        type="textarea"
        size="small"
        :rows="2"
        placeholder="请输入大屏简介"
        @change="changed($event, 'description')"
      />
    </el-form-item>

    <el-form-item label="背景颜色" prop="backgroundColor">
      <ColorPicker
        v-model.trim="formData.backgroundColor"
        @change="(val) => changed(val, 'backgroundColor')"
      />
    </el-form-item>
    <el-form-item label="背景图片" prop="backgroundImage">
      <UploadImg
        v-model="formData.backgroundImage"
        height="88px"
        width="auto"
        class="min-w-80px"
        @update:model-value="(val) => changed(val, 'backgroundImage')"
      />
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, watch } from 'vue'
import ColorPicker from '../../components/colorPicker.vue'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const formData = reactive<any>({})

const formRules = reactive({
  name: [{ required: true, message: '请输入大屏名称', trigger: ['blur', 'change'] }]
})

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>

<style lang="scss" scoped></style>
